export const setStorageSync = (key,value) => {
	try {
		uni.setStorageSync(key, value);
	} catch (e) {
		// error
	}
}

export const getStorageSync = (key) => {
	// return new Promise((resolve, reject) => {
		try {
			const value = uni.getStorageSync(key);
			return value || ''
		} catch (e) {
			// error
			reject('获取失败')
		}
	// })
	
}

export const removeStorageSync = (key) => {
	try {
		uni.removeStorageSync(key);
	} catch (e) {
		// error
	}
}

export const clearStorageSync = () => {
	try {
		uni.clearStorageSync();
	} catch (e) {
		// error
	}
}
export const callPhone = (phoneNumber) => {
      // 判断是否为字符串，并且是否为纯数字
      if (typeof phoneNumber === 'string' && /^\d+$/.test(phoneNumber)) {
        uni.makePhoneCall({
          phoneNumber: phoneNumber, // 电话号码
          success: function () {
            console.log('拨打电话成功');
          },
          fail: function () {
            console.log('拨打电话失败');
          }
        });
      } else {
        console.log('电话号码不正确');
		uni.showToast({
			icon:'none',
			title:'电话号码不正确'
		})
      }
    }

export const uploadFile = () => {
	return new Promise((resolve, reject) => {
		  const satoken  = getStorageSync('satoken') || '';
		
		uni.chooseImage({
			count: 1,
			mediaType: ['image'],
			sourceType: ['album', 'camera'],
			camera: 'back',
			
			success:(files) => {
				uni.showLoading({
					title:'上传中……',
					mask: true
				})
				console.log(files)
				uni.getFileSystemManager().readFile({
					encoding:'base64',
					filePath:files.tempFilePaths[0],
					success:(baseRes)=>{
              			resolve(baseRes.data)
					},
					complete:() => {
						uni.hideLoading()
					}
				})
				// uni.uploadFile({
				// 	url: 'https://iot.shunongyiqi.com/admin-api/infra/file/upload', 
				// 	filePath: files.tempFiles[0].path,
				// 	name: 'file',
				// 	header:{Authorization: `Bearer ${satoken}`},
				// 	success: (uploadFileRes) => {
				// 		if(uploadFileRes.data){
				// 			const data = JSON.parse(uploadFileRes.data)
				// 			resolve(data.data)
				// 		}
				// 	},
				// 	complete:() => {
				// 		uni.hideLoading()
				// 	}
				// });
			},
			fail:(err) => {
				console.log('---err',err)
				// uni.showLoading({
				// 	title:'fail:---'+JSON.stringify(err),
				// 	mask: true,
				// 	duration:3000
				// })
			}
			})
	})
}

export const formatTimestamp =(timestamp) => {
    // 创建一个 Date 对象
    const date = new Date(timestamp);

    // 获取年月日时分秒
    const year = date.getFullYear();
    const month = String(date.getMonth() + 1).padStart(2, '0'); // 月份从0开始，所以要加1
    const day = String(date.getDate()).padStart(2, '0');
    const hours = String(date.getHours()).padStart(2, '0');
    const minutes = String(date.getMinutes()).padStart(2, '0');
    const seconds = String(date.getSeconds()).padStart(2, '0');

    // 格式化为年月日时分秒
    return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
}
